Calculate Standard Deviation

Course- C >

This program calculates the standard deviation of individual series using arrays.

In this program, elements of arrays are used for storing the data and this array is passed to function which calculates standard deviation and finally the result(standard deviation) is displayed in main() function.

Source Code to Calculate Standard Deviation by Passing it to Function


/* Source code to calculate standard deviation. */

#include <stdio.h>
#include <math.h>
float standard_deviation(float data[], int n);
int main()
{
    int n, i;
    float data[100];
    printf("Enter number of datas( should be less than 100): ");
    scanf("%d",&n);
    printf("Enter elements: ");
    for(i=0; i<n; ++i)
        scanf("%f",&data[i]);
    printf("\n");
    printf("Standard Deviation = %.2f", standard_deviation(data,n));
    return 0;
}
float standard_deviation(float data[], int n)
{
    float mean=0.0, sum_deviation=0.0;
    int i;
    for(i=0; i<n;++i)
    {
        mean+=data[i];
    }
    mean=mean/n;
    for(i=0; i<n;++i)
    sum_deviation+=(data[i]-mean)*(data[i]-mean);
    return sqrt(sum_deviation/n);           
}

 

 
 

Output


Enter number of datas( should be less than 100): 6
Enter elements: 12
24.5
65.4
10.3
29.9
34.3